package cn.sdc.bugKiller.service;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import cn.sdc.bugKiller.bean.MemberBean;
import cn.sdc.bugKiller.bean.ProjectBean;

public class ContextService {
	
	public static final String STR_CUR_USER = "curruser";
	public static final String STR_CUR_PROJECT = "currprj";
	
	private static ServletContext context = null;
	private static HttpSession session = null;
	public static String resourceRootPath;
	
	public static void setServletContext(ServletContext sc){
		context = sc;
	}
	
	public static void setSession(HttpSession ses){
		if(session == null){
			session = ses;
		}
	}
	
	public static void setCurrentUser(MemberBean bean){
		if(session!=null)
		session.setAttribute(STR_CUR_USER, bean);
	}
	
	public static void clearSession(){
		if(session!=null){
			session.removeAttribute(STR_CUR_USER);
			session.removeAttribute(STR_CUR_PROJECT);
			session = null;
		}
	}
	
	public static void setCurrentProject(ProjectBean bean){
		if(session!=null)
		session.setAttribute(STR_CUR_PROJECT, bean);
	}
	
	public static MemberBean getCurrentUser(){
		if(session!=null){
			MemberBean bean = (MemberBean)session.getAttribute(STR_CUR_USER);
			if(bean==null || bean.getId()==null){
				return null;
			}
			return bean;
		}
		else
			return null;
	}
	
	public static Integer getCurrentUserId(){
		if(getCurrentUser()!=null)
			return getCurrentUser().getId();
		else
			return null;
	}
	
	public static ProjectBean getCurrentProject(){
		if(session!=null)
			return (ProjectBean)session.getAttribute(STR_CUR_PROJECT);
		else
			return null;
//			//only for test
//			return new ProjectBean(1,"EBS TEST PROJECT");
	}
	
	public static Integer getCurrentProjectId(){
		if(getCurrentProject()!=null)
			return getCurrentProject().getId();
		else
			return null;
	}
	
	public static void setResourceRootPath(){
		if(context!=null)
		resourceRootPath = context.getRealPath("/WEB-INF/classes");
	}
}
